<?php
namespace addon\mall\controller;
use app\common\controller\AddonAdminBase;
use tpfcore\Core;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * ============================================================================
 */
class GoodsCategory extends AddonAdminBase
{
    public function index(){
      	$this->addonTemplate('index',[
          "categorys"=>Core::loadAddonModel($this->name)->getListTree([
          		"order"=>"sort asc,id asc"
          ])
      	]);
    }
    public function add(){
      $GoodsCategoryLogic = Core::loadAddonModel($this->name);
      $GoodsAttributeLogic = Core::loadAddonModel("GoodsAttribute");

      IS_POST && $this->jump($GoodsCategoryLogic->saveGoodsCategory($this->param));
      $listGoodsType = Core::loadAddonModel("GoodsType")->getGoodsType([
        "where"=>["status"=>1]
      ]);
      $gtids = [];
      foreach ($listGoodsType as $key => $value) {
        $gtids[]=$value['id'];
      }
      if($gtids){
        $listGoodsAttribute = $GoodsAttributeLogic->getGoodsAttribute([
          "field"=>"id,name,type_id",
          "where"=>["type_id"=>["exp"," in (".implode(",",$gtids).")"]]
        ]);
      }

      // 编辑分类
      if(isset($this->param['id'])){   
        $filter_attr_list = [];
        $list = $GoodsCategoryLogic->getGoodsCategory(["where"=>["id"=>$this->param['id']]]);
        $this->param['parentid']=$list[0]->parent_id;

        $filter_attr = $list[0]->filter_attr;
        if(!empty($filter_attr)){
          $filter_attr_arr = explode(",", $filter_attr);
          foreach ($filter_attr_arr as $key => $value) {
            if($value == 0) continue;
            $listGAB = $GoodsAttributeLogic->getGoodsAttribute([
              "field"=>"type_id",
              "where"=>["id"=>$value]
            ]);
            $filter_attr_list[$key]['goods_type_list']=$this->goods_type_list($listGoodsAttribute,$listGAB[0]->type_id);
            $filter_attr_list[$key]['filter_attr']=$value;
            $filter_attr_list[$key]['type_id']=$listGAB[0]->type_id;
          }
        }
        $this->assign("list",$list);
        $this->assign("filter_attr_list",$filter_attr_list);
      }

      $this->addonTemplate('add_edit',[
        "categorys"=>$GoodsCategoryLogic->getTreeGoodsCategory($this->param),
        "listGoodsType"=>$listGoodsType,
        "listGoodsAttribute"=>empty($listGoodsAttribute)?[]:$listGoodsAttribute
      ]);
    }
    public function del(){
    	 $this->jump(Core::loadAddonModel($this->name)->delGoodsCategory($this->param));
    }
    private function goods_type_list($listGoodsAttribute,$type_id){
      $arr = [];
      foreach ($listGoodsAttribute as $key => $value) {
        if($value['type_id'] == $type_id){
          $arr[$value['id']] = $value['name'];
        }
      }
      return $arr;
    }
}
